package com.project.web.admin.controller.core;

import com.github.pagehelper.PageInfo;
import com.project.model.core.Menu;
import com.project.model.core.Role;
import com.project.web.admin.controller.common.BaseController;
import com.project.web.admin.service.core.MenuService;
import com.project.web.admin.service.core.RoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.yun.common.utils.appInterface.Message;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("adminRoleController")
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

    private static final long serialVersionUID = -1728110105254386066L;
    @Resource(name = "roleServiceImpl")
    public RoleService roleService;

    @Resource(name = "menuServiceImpl")
    public MenuService menuService;

    /**
     * 列表
     *
     * @param pageInfo
     * @param role
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageInfo<Role> pageInfo, Role role, Model model) {
        model.addAttribute("page", roleService.selectList(role, pageInfo));
        return "/intfc/core/role/list.ftl";
    }

    /**
     * 添加
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        return "/intfc/core/role/add.ftl";
    }

    /**
     * 编辑
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        Role role = roleService.getById(id);
        String menuIds = roleService.selectMenuIds(id);
        role.setMenuIds(menuIds);
        model.addAttribute("role", role);
        return "/intfc/core/role/edit.ftl";
    }

    /**
     * 更新
     *
     * @param role
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Role role, String menuIds, RedirectAttributes redirectAttributes) {
        if (role != null) {
            roleService.updateRoleMenu(role, menuIds);
            addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        } else {
            addFlashMessage(redirectAttributes, ERROR_MESSAGE);
        }
        return "redirect:list.jhtml";
    }

    /**
     * 删除
     *
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Long id, RedirectAttributes redirectAttributes) {
        Role role = roleService.getById(id);
        if (BooleanUtils.isTrue(role.getIsSystem())) {
            addFlashMessage(redirectAttributes, Message.error("系统内置角色不能删除"));
        } else {
            roleService.deleteRef(id);
            addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        }
        return "redirect:list.jhtml";
    }

    /**
     * 跳到权限树页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/menuTree", method = RequestMethod.GET)
    public String menuTree(Model model, Long id) {
        Role role = roleService.getById(id);
        if (role == null) {
            role = new Role();
        }
        model.addAttribute(role);
        return "/intfc/core/role/menuTree.ftl";
    }

    /**
     * 载入树形页面
     *
     * @param id
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editRoleMenu", method = RequestMethod.POST)
    public Map<String, Object> tree(Long id, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleMenus", menuService.selectMenusByRoleId(id));
        map.put("menus", menuService.selectList(new Menu()));
        map.put("success", true);
        return map;
    }

    /**
     * 保存
     *
     * @param role
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Role role, String menuIds, RedirectAttributes redirectAttributes) {
        role.setIsSystem(false);
        roleService.saveRoleMenu(role, menuIds);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 禁用
     *
     * @param id
     * @param status
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/disabledRole", method = RequestMethod.GET)
    public String disabledRole(Long id, Integer status, RedirectAttributes redirectAttributes) {
        Role roleTemp = new Role();
        roleTemp.setId(id);
        if (BooleanUtils.isTrue(roleService.getById(id).getIsSystem())) {
            addFlashMessage(redirectAttributes, Message.error("系统内置角色不能禁用"));
            return "redirect:list.jhtml";
        }
        if (status != null && status == 0) {
            roleTemp.setStatus(1);
        }
        if (status != null && status == 1) {
            roleTemp.setStatus(0);
        }
        roleService.update(roleTemp);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * roleCode唯一性校验
     *
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/validateRoleCode", method = RequestMethod.POST)
    public Boolean validateRoleCode(Role role) {
        String roleCode = role.getRoleCode();
        Long id = role.getId();
        Role roleTemp = new Role();
        if (StringUtils.isNotBlank(roleCode)) {
            roleTemp.setRoleCode(roleCode);
        }
        if (id != null) {
            roleTemp.setId(id);
        }
        List<Role> roleList = roleService.validateRoleCode(roleTemp);
        return CollectionUtils.isEmpty(roleList);
    }

}
